﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;

namespace SysComm
{
    /// <summary>
    /// 文件加密运算类
    /// </summary>
    public class DocSecurity
    {

        /// <summary>
        /// 文件加密
        /// </summary>
        /// <param name="filePath">要加密的文件</param>
        /// <param name="outPath">加密后的文件</param>
        /// <param name="pass">加密的密钥</param>
        /// <param name="isDel">加密成功后是否要删除原有的文件</param>
        /// <returns>0为成功;非0失败</returns>
        public static int Encrypt(string filePath, string outPath, string pass, bool isDel)
        {
            int ret = 0;
            try
            {
                byte[] rgbKey = new byte[0];
                if (pass.Length == 6)
                {
                    rgbKey = new byte[] { (byte)pass[0], (byte)pass[1], (byte)pass[2], (byte)pass[3], (byte)pass[4], (byte)pass[5], (byte)pass[0], (byte)pass[1] };
                }
                if (pass.Length == 7)
                {
                    rgbKey = new byte[] { (byte)pass[0], (byte)pass[1], (byte)pass[2], (byte)pass[3], (byte)pass[4], (byte)pass[5], (byte)pass[6], (byte)pass[0] };
                }
                if (pass.Length >= 8)
                {
                    rgbKey = new byte[] { (byte)pass[0], (byte)pass[1], (byte)pass[2], (byte)pass[3], (byte)pass[4], (byte)pass[5], (byte)pass[6], (byte)pass[7] };
                }
                FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                FileStream stream2 = new FileStream(outPath, FileMode.OpenOrCreate, FileAccess.Write);
                stream2.SetLength((long)0);
                byte[] buffer = new byte[0x400];
                int num = 0;
                long length = stream.Length;
                int num3 = (int)(length / ((long)0x400));
                DES des = new DESCryptoServiceProvider();
                CryptoStream stream3 = new CryptoStream(stream2, des.CreateEncryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
                while (num < length)
                {
                    int count = stream.Read(buffer, 0, 0x400);
                    stream3.Write(buffer, 0, count);
                    num += count;
                }
                stream3.Close();
                stream2.Close();
                stream.Close();
                try
                {
                    if (isDel)
                    {
                        File.Delete(filePath);
                    }
                }
                catch
                {
                    ret = -1;
                }
            }
            catch
            {
                ret = -1;
            }

            return ret;
        }


        /// <summary>
        /// 文件解密
        /// </summary>
        /// <param name="filePath">要解密的文件</param>
        /// <param name="outPath">解密后的文件</param>
        /// <param name="pass">密钥</param>
        /// <param name="isDel">是否要删除要解密的文件</param>
        /// <returns>0为成功;非0为失败</returns>
        public static int Decrypt(string filePath, string outPath, string pass, bool isDel)
        {
            int ret = 0;
            CryptoStream stream = null;
            FileStream stream2 = null;
            FileStream stream3 = null;
            try
            {
                try
                {
                    byte[] rgbKey = new byte[0];
                    if (pass.Length == 6)
                    {
                        rgbKey = new byte[] { (byte)pass[0], (byte)pass[1], (byte)pass[2], (byte)pass[3], (byte)pass[4], (byte)pass[5], (byte)pass[0], (byte)pass[1] };
                    }
                    if (pass.Length == 7)
                    {
                        rgbKey = new byte[] { (byte)pass[0], (byte)pass[1], (byte)pass[2], (byte)pass[3], (byte)pass[4], (byte)pass[5], (byte)pass[6], (byte)pass[0] };
                    }
                    if (pass.Length >= 8)
                    {
                        rgbKey = new byte[] { (byte)pass[0], (byte)pass[1], (byte)pass[2], (byte)pass[3], (byte)pass[4], (byte)pass[5], (byte)pass[6], (byte)pass[7] };
                    }
                    stream2 = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    FileInfo info = new FileInfo(filePath);
                    stream3 = new FileStream(outPath, FileMode.OpenOrCreate, FileAccess.Write);
                    stream3.SetLength((long)0);
                    byte[] buffer = new byte[0x400];
                    int num = 0;
                    long length = stream2.Length;
                    int num3 = (int)(length / ((long)0x400));
                    DES des = new DESCryptoServiceProvider();
                    stream = new CryptoStream(stream3, des.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
                    while (num < length)
                    {
                        int count = stream2.Read(buffer, 0, 0x400);
                        stream.Write(buffer, 0, count);
                        num += count;
                    }
                    stream.Close();
                    stream3.Close();
                    stream2.Flush();
                    stream2.Close();
                    try
                    {
                        if (isDel)
                        {
                            File.Delete(filePath);
                        }
                    }
                    catch
                    {
                    }
                }
                catch
                {
                    stream2.Close();
                    stream3.Flush();
                    stream3.Close();
                    if (File.Exists(outPath))
                    {
                        File.Delete(outPath);
                    }
                    ret = -1;
                }
            }
            finally
            {
                stream2.Close();
                stream3.Close();
            }

            return ret;
        }

    }
}
